package ar.com.drconta.ganancias.views.vogella;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class View2  extends ViewPart {
  public static final String ID = "de.vogella.jface.treeviewer.view";
  private TreeViewer viewer;

//  @PostConstruct
  public void createPartControl(Composite parent) {
    viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    viewer.setContentProvider(new TodoContentProvider());
    viewer.setLabelProvider(new TodoLabelProvider());
    // Expand the tree
    viewer.setAutoExpandLevel(2);
    // provide the input to the ContentProvider
    viewer.setInput(new TodoMockModel());

    // add a doubleclicklistener
    viewer.addDoubleClickListener(new IDoubleClickListener() {
		
      @Override
      public void doubleClick(DoubleClickEvent event) {
        TreeViewer viewer = (TreeViewer) event.getViewer();
        IStructuredSelection thisSelection = (IStructuredSelection) event
            .getSelection();
        Object selectedNode = thisSelection.getFirstElement();
        viewer.setExpandedState(selectedNode,
            !viewer.getExpandedState(selectedNode));
      }
    });

    viewer.getTree().addKeyListener(new KeyAdapter() {

      @Override
      public void keyReleased(final KeyEvent e) {
    	  System.out.println("keyt pressed");
        if (e.keyCode == SWT.DEL) {
          final IStructuredSelection selection = (IStructuredSelection) viewer
              .getSelection();
          if (selection.getFirstElement() instanceof Todo) {
            Todo o = (Todo) selection.getFirstElement();
            // TODO Delete the selected element from the model
          }

        }
      }
    });

  }

//  @Focus
  public void setFocus() {
    viewer.getControl().setFocus();
  }
} 